RPC & Server-side Game Logic
This document describes RPC usage in the current PlayServ Unity SDK.
Event payload used in examples
using Playserv.Events;
public sealed class NotificationEvent : Event
{
}RPC service contract
RPC service types must be marked with [Rpc].
using System;
using Playserv.RPC;
using Playserv.Wrapper;
[Rpc]
public sealed class NotificationService
{
public void BroadcastToAll(string message)
{
PlayServ.Publish(new NotificationEvent
{
EventId = Guid.NewGuid().ToString(),
Message = message,
Timestamp = DateTime.UtcNow,
EventType = "Broadcast"
});
}
}Invoke from client/runtime
By service + method names:
PlayServ.Invoke(
nameof(NotificationService),
nameof(NotificationService.BroadcastToAll),
new { message = "Hello from client" });By expression:
PlayServ.Invoke<NotificationService>(x => x.BroadcastToAll("Hello"));With pre-encoded base64 payload:
PlayServ.Invoke(
"NotificationService",
"BroadcastToAll",
"WyJIZWxsbyJd");In-process server mode
Register local invoker to execute RPC without websocket:
using System;
using Playserv.RPC;
using Playserv.Wrapper;
var rpcInvoker = new LocalRpcInvoker()
.RegisterService(new NotificationService());
PlayServ.SetRpcInvoker(rpcInvoker);Payload mapping
When using object payload (new { message = "..." }), fields are mapped by RPC method parameter names.
If payload is missing required parameters, local invoker throws descriptive exceptions.
Important notes
[Rpc]attribute is required for service registration/invoke-by-expression.- If local invoker does not handle call and transport is not connected, SDK throws
InvalidOperationException. - Current invoke API is fire-and-forward; response patterns are typically implemented via events.
Summary
RPC in current SDK is built around [Rpc] service contracts and PlayServ.Invoke(...), with optional in-process execution through LocalRpcInvoker.
Last updated on